package com.letoken.platform.repo.mall.dao;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.letoken.platform.repo.app.req.mall.EvaluateListReq;
import com.letoken.platform.repo.app.req.mall.MyEvaluateReq;
import com.letoken.platform.repo.app.req.mall.QueryMallOrderHistoryReq;
import com.letoken.platform.repo.app.req.mall.UserQueryEvaluateReq;
import com.letoken.platform.repo.app.res.mall.*;
import com.letoken.platform.repo.dao.SpiceBaseMapper;
import com.letoken.platform.repo.mall.po.AppMallEvaluate;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.letoken.platform.repo.mall.po.AppMallOrderInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 店铺评价表 Mapper 接口
 * </p>
 *
 * @author author
 * @since 2023-07-03
 */
public interface AppMallEvaluateMapper extends SpiceBaseMapper<AppMallEvaluate> {

    @Select("select id,mall_id,user_id,order_id,score,anonymous from app_mall_evaluate where id = #{id} and  user_id = #{userId} and state = 0 ")
    AppMallEvaluate evaluateIdIsExits(@Param("userId") Integer userId, @Param("id") Integer id);


    @Select("select id,mall_id,order_id from app_mall_evaluate where id = #{id} and  mall_id = #{mallId} and state = 0  and reply_state = 0 ")
    AppMallEvaluate evaluateMallIdIsExits(@Param("mallId") Integer mallId, @Param("id") Integer id);



    @Select("select count(*) from app_mall_evaluate where order_id = #{orderId} and  user_id = #{userId} limit 1")
    int evaluateCount(@Param("userId") Integer userId, @Param("orderId") Integer orderId);

    @Select("select id,mall_id,user_id,state from app_mall_order_info where id = #{id} and  user_id = #{userId} ")
    AppMallOrderInfo evaluateOrder(@Param("userId") Integer userId, @Param("id") Integer id);


    @Select("select 1 from app_mall_evaluate where order_id = #{orderId} and  state = 0 limit 1 ")
    Integer evaluateIsExits(@Param("orderId") Integer orderId);



    Page<MyEvaluateRes> myEvaluate(@Param("page") Page<MyEvaluateRes> page , @Param("req") MyEvaluateReq req);



    @Select("select sum(score)/count(1) from app_mall_evaluate where mall_id = #{mallId} and state in (0,2)")
    Float averageScore(@Param("mallId") Integer mallId);

    @Select("select count(1) from app_mall_evaluate where mall_id = #{mallId} and state = 0")
    Integer evaluateNum(@Param("mallId") Integer mallId);


    @Select("select count(1) from app_mall_evaluate where mall_id = #{mallId} and score >= #{score} and state = 0")
    Integer evaluateNumByScore(@Param("mallId") Integer mallId,@Param("score") Float score);

    @Select("select count(1) from app_mall_evaluate where mall_id = #{mallId} and create_time > #{date} and state = 0")
    Integer dayEvaluateNum(@Param("mallId") Integer mallId, @Param("date")LocalDateTime date);



    Integer conditionEvaluateNum( @Param("req") EvaluateListReq req);

    Page<EvaluateListRes> evaluateList(@Param("page") Page<EvaluateListRes> page , @Param("req") EvaluateListReq req);


    @Select("select e.mall_id as mallId ,sum(e.score) as score ,m.is_business as isBusiness,m.level as level,m.level_score as levelScore from app_mall_evaluate e inner join app_mall_info m on e.mall_id = m.id where e.state in (0,2) group by e.mall_id ")
    List<TaskScoreVo> allMallEvaluateScore();



    Page<DetailEvaluateRes> userQueryEvaluateList(@Param("page") Page<DetailEvaluateRes> page , @Param("req") UserQueryEvaluateReq req);


    @Select("select  (select count(*) from app_mall_evaluate where mall_id = #{mallId} and score >= 3.0 and state in (0,2) ) / (select count(*) from app_mall_evaluate where mall_id = #{mallId} and state in (0,2) )")
    Float satisfaction(@Param("mallId") Integer mallId);

}
